package com.pg.controller.dd;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pg.entity.EipTemp;
import com.pg.entity.cj.Jjd;
import com.pg.entity.dd.EipSalesOrder;
import com.pg.service.EipSalesOrderService;
import com.pg.service.EipTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售订单前端控制器
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Api(tags = "销售订单")
@RestController
@RequestMapping("/eipSalesOrder")
@Slf4j
public class EipSalesOrderController extends JeecgController<EipSalesOrder, EipSalesOrderService> {

    @Autowired
    private EipTempService eipTempService;

    @Autowired
    private EipSalesOrderService eipSalesOrderService;

    /**
     * 添加 销售订单
     *
     * @param eipSalesOrder 销售订单
     * @return 是否成功
     */
    @ApiOperation("添加 销售订单")
    @AutoLog(value = "添加 销售订单")
    @PostMapping("/add")
    public Result<?> add(@RequestBody EipSalesOrder eipSalesOrder) {
        eipSalesOrder.setPuid(UUID.randomUUID().toString());
        eipSalesOrderService.save(eipSalesOrder);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑 销售订单
     *
     * @param eipSalesOrder 销售订单
     * @return 是否成功
     */
    @ApiOperation("编辑 销售订单")
    @AutoLog(value = "编辑 销售订单")
    @PostMapping("/edit")
    public Result<?> edit(@RequestBody EipSalesOrder eipSalesOrder) {
        eipSalesOrderService.save(eipSalesOrder);
        return Result.ok("编辑成功！");
    }

    /**
     * 临时编辑 销售订单
     *
     * @param eipSalesOrder 销售订单
     * @return 是否成功
     */
    @ApiOperation("临时编辑 销售订单")
    @AutoLog(value = "DataCheck")
    @RequestMapping(method = RequestMethod.POST, value = "/editTemp")
    public Result<?> editTemp(@Valid @RequestBody EipSalesOrder eipSalesOrder, HttpServletRequest req) {
        String userName = (String) req.getSession().getAttribute("userName");
        EipTemp eipTemp = new EipTemp();
        eipTemp.setDataType("销售订单");
        eipTemp.setModUser(userName);
        eipTemp.setModTime(new Date());
        eipTemp.setLinkPUID(eipSalesOrder.getPuid());
        eipTemp.setDetailedInformation(JSONObject.toJSONString(eipSalesOrder));
        try {
            eipSalesOrderService.updateSalesOrder(eipTemp);
            return Result.ok("编辑成功!");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("编辑失败!");
        }
    }

    /**
     * 通过id删除 销售订单
     *
     * @param id 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id删除 销售订单")
    @AutoLog(value = "通过id删除 销售订单")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        eipSalesOrderService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除 销售订单
     *
     * @param ids 全库唯一码集合
     * @return 是否成功
     */
    @ApiOperation("批量删除 销售订单")
    @AutoLog(value = "批量删除 销售订单")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.eipSalesOrderService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id逻辑删除 销售订单
     *
     * @param id 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id逻辑删除 销售订单")
    @AutoLog(value = "通过id逻辑删除 销售订单")
    @DeleteMapping(value = "/logicalDelete")
    public Result<?> logicalDelete(@RequestParam(name = "id", required = true) String id) {
        eipSalesOrderService.logicalDelete(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量逻辑删除 销售订单
     *
     * @param ids 全库唯一码集合
     * @return 是否成功
     */
    @ApiOperation("批量逻辑删除 销售订单")
    @AutoLog(value = "批量逻辑删除 销售订单")
    @DeleteMapping(value = "/logicalDeleteBatch")
    public Result<?> logicalDeleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        eipSalesOrderService.logicalDeleteBatch(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询 销售订单
     *
     * @param puid 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id查询 销售订单")
    @AutoLog(value = "DataCheck")
    @RequestMapping(method = RequestMethod.GET, value = "/selectByPuid")
    public Result<?> selectByPuid(@RequestParam(name = "puid") String puid) {
        EipSalesOrder byId;
        byId = eipTempService.selectSalesOrderByLinkPUID(puid);
        if (byId == null) {
            byId = eipSalesOrderService.selectByPuid(puid);
            if (byId == null) {
                return Result.error("未找到对应数据");
            }
        }
        return Result.ok(byId);
    }

    /**
     * 分页列表查询 销售订单
     *
     * @param eipSalesOrder 销售订单
     * @param pageNo        起始页
     * @param pageSize      每页页数
     * @param req           参数
     * @return 分页数据
     */
    @ApiOperation("分页列表查询 销售订单")
    @AutoLog(value = "QueryExport")
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public Result<?> selectForList(EipSalesOrder eipSalesOrder,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<EipSalesOrder> page = new Page<EipSalesOrder>(pageNo, pageSize);
        page.setRecords(eipSalesOrderService.selectForList(page, eipSalesOrder));
        return Result.ok(page);
    }

    /**
     * 分页列表查询 销售订单
     *
     * @param eipSalesOrder 销售订单
     * @param pageNo        起始页
     * @param pageSize      每页页数
     * @param req           参数
     * @return 分页数据
     */
    @ApiOperation("分页列表查询 销售订单")
    @AutoLog(value = "DataCheck")
    @RequestMapping(method = RequestMethod.GET, value = "/checkList")
    public Result<?> checkList(EipSalesOrder eipSalesOrder,
                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                               HttpServletRequest req) {
        Page<EipSalesOrder> page = new Page<EipSalesOrder>(pageNo, pageSize);
        page.setRecords(eipSalesOrderService.selectForList(page, eipSalesOrder));
        return Result.ok(page);
    }

    /**
     * 通过excel导入数据 销售订单
     *
     * @param request  参数
     * @param response 响应
     * @return 是否成功
     */
    @ApiOperation("通过excel导入数据 销售订单")
    @AutoLog(value = "通过excel导入数据 销售订单")
    @RequestMapping(method = RequestMethod.POST, value = "/importExcel")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EipSalesOrder.class);
    }

    /**
     * 导出excel 销售订单
     *
     * @param request       参数
     * @param eipSalesOrder 销售订单
     */
    @ApiOperation("导出excel 销售订单")
    @AutoLog(value = "QueryExport")
    @RequestMapping(method = RequestMethod.GET, value = "/exportXls")
    public ModelAndView exportXls(EipSalesOrder eipSalesOrder, HttpServletRequest request) {
        // Step.1 组装查询条件
        QueryWrapper<EipSalesOrder> queryWrapper = QueryGenerator.initQueryWrapper(eipSalesOrder, request.getParameterMap());
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //update-begin--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据--------------------
        String selections = request.getParameter("selections");
        if (!oConvertUtils.isEmpty(selections)) {
            queryWrapper.in("puid", (Object) selections.split(","));
        }
        List<EipSalesOrder> pageList = eipSalesOrderService.selectForList(null,eipSalesOrder);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "销售订单列表");
        mv.addObject(NormalExcelConstants.CLASS, EipSalesOrder.class);
//        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("洁净度列表数据", "导出人:" + user.getRealname(), "导出信息"));
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("销售订单数据", "导出人:" + request.getSession().getAttribute("userName"), "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }
}
